package com.z.controller;

import com.z.pojo.Goods;
import com.z.service.GoodsService;
import com.z.service.impl.GoodsServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 张福兴
 * date 2021/9/8
 */
@WebServlet("/addGoods")
public class AddGoodsServlet extends HttpServlet {
    private final GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        FileItemFactory fileItemFactory =new DiskFileItemFactory();
        ServletFileUpload sfu  = new ServletFileUpload(fileItemFactory);
        List<FileItem> fileItems=null;
        try {
            fileItems = sfu.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        FileItem fileItem = fileItems.get(0);
        String gname = fileItem.getString("utf-8");

        FileItem fileItem1 = fileItems.get(1);
        String  tid  =fileItem1.getString("utf-8");

        FileItem fileItem2 = fileItems.get(2);
        String filename = fileItem2.getName();

        FileItem fileItem3 = fileItems.get(3);
        String price =fileItem3.getString("utf-8");

        FileItem fileItem4 = fileItems.get(4);
        String gnum =fileItem4.getString("utf-8");

        FileItem fileItem5 = fileItems.get(5);
        String pdesc =fileItem5.getString("utf-8");

//        1.确定我们上传图片的位置存在。
        String savepath = req.getContextPath()+"/gimages/";
        System.out.println(savepath);
        File savefile = new File(savepath);
        if(!savefile.isDirectory()){
            if(savefile.exists()){
                savefile.delete();
                savefile.mkdir();
            }else {
                savefile.mkdir();
            }
        }
//      2.修改上传图片的名称。
//            1.确定上传文件名称没有路径/
//              1.windows linux  mac。
        if(filename.indexOf(File.separator)>-1){
            filename =filename.substring(filename.lastIndexOf(File.separator));
        }
//      修改名称
        String newName = UUID.randomUUID().toString()+"_"+ System.currentTimeMillis() +"_"+filename;
        File save = new File(savepath,newName);
        try {
            fileItem2.write(save);
        } catch (Exception e) {
            e.printStackTrace();
        }
//      使用路径

        Goods goods = new Goods();

        goods.setPrice(Double.parseDouble(price));
        goods.setGname(gname);
        goods.setG_img(newName);
        goods.setGdesc(pdesc);
        goods.setGnum(Integer.parseInt(gnum));
        goods.setTid(Integer.parseInt(tid));


        Integer flag = goodsService.addGoods(goods);
        if(flag > 0){
            req.setAttribute("msg","添加成功");
        }else {
            req.setAttribute("msg","添加失败");
        }
        req.getRequestDispatcher("/manageGoods").forward(req,resp);
    }
}
